package com.lay.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lay.entity.*;
import com.lay.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/yingxiao")
public class YingXiaoguanliController {
    @Autowired
    //保险信息
    private CarInsurService carInsurService;
    @Autowired
    //保险套餐
    private InsurPackService insurPackService;
    //保险清单
    @Autowired
    private InserIncludeService includeService;
    /**
     * @title
     * @description 跳转到新增保险套餐页面
     * @author admin
     * @param
     * @updateTime 2022/6/10 10:23
     * @return

     */
    @RequestMapping("toyingxiao-add")
    public ModelAndView yingXiaoAdd(){
        ModelAndView mv =new ModelAndView();
        QueryWrapper qw = new QueryWrapper();
        qw.eq("ci_state",1);
        List list = carInsurService.list(qw);
        if (list.size() >0){
            mv.addObject("alist", list);
            mv.setViewName("yingxiao-add");
        }
        return mv;
    }
    /**
     * @title
     * @description 查询套餐名字是否重复
     * @author admin
     * @param
     * @updateTime 2022/6/10 10:22
     * @return

     */
    @RequestMapping("/querytaocan")
    @ResponseBody
    public String querytaocan(String ipName){
        QueryWrapper qw = new QueryWrapper();
        qw.eq("ip_name",ipName);
        InsurPack insurPack = insurPackService.getOne(qw);
        if (insurPack != null){
            return "1";
        }
        return "0";
    }
    /*
     * @title addtaocan
     * @description 添加保险套餐
     * @author admin
     * @param
     * @updateTime 2022/6/10 10:22
     * @return 是否成功
     */
    @RequestMapping("/addtaocan")
    @ResponseBody
    public String addtaocan(InserInclude include, InsurPack insurPack, HttpSession session){
        includeService.save(include);
        Integer iiId = include.getIiId();
        UserNumber users = (UserNumber) session.getAttribute("users");
        Integer unId = users.getUnId();
        insurPack.setUnId(unId);
        insurPack.setIiId(iiId);
        insurPack.setIpState(2);
        boolean b = insurPackService.save(insurPack);
        if (b){
            return "1";
        }
        return "0";
    }
    /**
     * @title
     * @description 跳转到保险套餐管理页面
     * @author admin
     * @param
     * @updateTime 2022/6/10 13:55
     * @return

     */
    @RequestMapping("/topicture-list")
    public ModelAndView pictureList(){
        ModelAndView mv = new ModelAndView();
        List<InsurPack> list = insurPackService.getAll();
        mv.addObject("list",list);
        mv.addObject("count",list.size());
        if (list.size() >0){
            mv.setViewName("picture-list");
            return mv;
        }
        return mv;
    }
    /**
     * @title
     * @description 修改套餐状态 发布或者下架
     * @author admin
     * @param
     * @updateTime 2022/6/10 15:08
     * @return
     */
    @RequestMapping("/edittaocan")
    @ResponseBody
    public String edittaocan(InsurPack insurPack){
        boolean b = insurPackService.updateById(insurPack);
        if (b){
            return "1";
        }
        return "0";
    }
}
